package com.mmy.controller.train;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mmy.controller.Common;
import com.mmy.controller.UtilConstants;
import com.mmy.domain.scenic.VScenicOrder;
import com.mmy.domain.sys.TSysUser;
import com.mmy.domain.train.TTrainBase;
import com.mmy.domain.train.TTrainOrder;
import com.mmy.domain.train.TTrainPlan;
import com.mmy.domain.train.VTrainOrder;
import com.mmy.domain.train.VTrainPlan;
import com.mmy.service.scenic.impl.ScenicOrderServiceImpl;
import com.mmy.service.train.impl.TrainBaseServiceImpl;
import com.mmy.service.train.impl.TrainOrderServiceImpl;
import com.mmy.service.train.impl.TrainPlanServiceImpl;

/**
 * 类的介绍：火车票订单
 *
 * @author shangzl
 * @date 创建日期：
 * @version 1.0
 */
@Controller
public class TrainOrderController {
	// Hanxl 整理 2020.10.29
	@Autowired
	TrainOrderServiceImpl trainOrderServiceImpl;
	@Autowired
	TrainBaseServiceImpl trainBaseServiceImpl;
	@Autowired
	TrainPlanServiceImpl trainPlanServiceImpl;
	@Autowired
	Common common;
	@Autowired
	ScenicOrderServiceImpl scenicOrderServiceImpl;

	@RequestMapping(value = "/totrainallorderindex")
	public String toTrainOrderIndex(HttpServletRequest request, Model model, String pageNumber, String pageSize,
			HttpSession session) {
		TSysUser sessionSysUser = (TSysUser) session.getAttribute("tSysUser");
		String strScenicName = request.getParameter("strScenicName");
		String strOrderName = request.getParameter("strOrderName");
		String strOrderTel = request.getParameter("strOrderTel");
		String strOrderDate = request.getParameter("strOrderDate");
		String strOrderState = request.getParameter("strOrderState");

		List<VScenicOrder> lstScenicOrder = new ArrayList<VScenicOrder>();
		HashMap<String, Integer> mapPage = new HashMap<String, Integer>();
		// 管理员登录 默认查看所有 并分页 带搜索条件
		if (sessionSysUser.getUserID() == 0) {
			lstScenicOrder = scenicOrderServiceImpl.findBySeachAdministrator(pageNumber, pageSize, strScenicName,
					strOrderName, strOrderTel, strOrderDate, strOrderState);
			mapPage = scenicOrderServiceImpl.mapSeachAdministrator(pageNumber, pageSize, strScenicName, strOrderName,
					strOrderTel, strOrderDate, strOrderState);
		} else {
			// 非管理员登录 默认查看自家公司的景区所有订单 以及订单操作人是自己公司的订单
			// 搜索时 输入搜索条件同时满足上述条件
			lstScenicOrder = scenicOrderServiceImpl.findBySearchOperator(pageNumber, pageSize, strScenicName,
					strOrderName, strOrderTel, strOrderDate, strOrderState, sessionSysUser);
			mapPage = scenicOrderServiceImpl.mapSearchOperator(pageNumber, pageSize, strScenicName, strOrderName,
					strOrderTel, strOrderDate, strOrderState, sessionSysUser);
		}

		model.addAttribute("lstScenicOrder", lstScenicOrder);

		model.addAttribute("iPageNo", mapPage.get("iPageNo"));
		model.addAttribute("iTotalCount", mapPage.get("iTotalCount"));
		model.addAttribute("iMaxPage", mapPage.get("iMaxPage"));
		model.addAttribute("iPageSize", mapPage.get("iPageSize"));
		model.addAttribute("strScenicName", strScenicName);
		model.addAttribute("strOrderName", strOrderName);
		model.addAttribute("strOrderTel", strOrderTel);
		model.addAttribute("strOrderDate", strOrderDate);
		model.addAttribute("strOrderState", strOrderState);
		return "webbackground/train/TrainAllOrderIndex";
	}

	/**
	 * 函数功能说明：后台显示未使用的订单信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.12.29
	 */
	@RequestMapping(value = "/totrainunusedorderindex")
	public String toTrainUnUsedOrderIndex(HttpServletRequest request, Model model, String pageNumber, String pageSize,
			HttpSession session) {

		TSysUser sessionSysUser = (TSysUser) session.getAttribute("tSysUser");
		String strScenicName = request.getParameter("strScenicName");
		String strOrderName = request.getParameter("strOrderName");
		String strOrderTel = request.getParameter("strOrderTel");
		String strOrderDate = request.getParameter("strOrderDate");
		List<VScenicOrder> lstScenicOrder = new ArrayList<VScenicOrder>();
		HashMap<String, Integer> mapPage = new HashMap<String, Integer>();

		// 管理员登录 默认查看所有 并分页 带搜索条件
		if (sessionSysUser.getUserID() == 0) {
			lstScenicOrder = scenicOrderServiceImpl.findBySeachAdministratorUnUsed(pageNumber, pageSize, strScenicName,
					strOrderName, strOrderTel, strOrderDate);
			mapPage = scenicOrderServiceImpl.mapSeachAdministratorUnUsed(pageNumber, pageSize, strScenicName,
					strOrderName, strOrderTel, strOrderDate);
		} else {
			// 非管理员登录 默认查看自家公司的景区所有订单 以及订单操作人是自己公司的订单
			// 搜索时 输入搜索条件同时满足上述条件
			lstScenicOrder = scenicOrderServiceImpl.findBySearchOperatorUnUsed(pageNumber, pageSize, strScenicName,
					strOrderName, strOrderTel, strOrderDate, sessionSysUser);
			mapPage = scenicOrderServiceImpl.mapSearchOperatorUnUsed(pageNumber, pageSize, strScenicName, strOrderName,
					strOrderTel, strOrderDate, sessionSysUser);
		}

		model.addAttribute("lstScenicOrder", lstScenicOrder);

		model.addAttribute("iPageNo", mapPage.get("iPageNo"));
		model.addAttribute("iTotalCount", mapPage.get("iTotalCount"));
		model.addAttribute("iMaxPage", mapPage.get("iMaxPage"));
		model.addAttribute("iPageSize", mapPage.get("iPageSize"));
		model.addAttribute("strScenicName", strScenicName);
		model.addAttribute("strOrderName", strOrderName);
		model.addAttribute("strOrderTel", strOrderTel);
		model.addAttribute("strOrderDate", strOrderDate);
		return "webbackground/train/TrainUnUsedOrderIndex";
	}

	@RequestMapping(value = "/subreservetrainbase")
	public String ReserveTrainBase(TTrainOrder tTrainOrder, HttpSession session, String strPlanID, String TrainID)
			throws Exception {
		// 获取最大ID
		tTrainOrder.setID(common.createMaxID(UtilConstants.TableName.T_TRAIN_ORDER));

		// 获得订单编号
		int iPlanID = Integer.parseInt(strPlanID);
		String cOrderID = common.createOrderCode(UtilConstants.TableName.T_TRAIN_ORDER, iPlanID);

		try {
			TTrainPlan tTrainPlan = trainPlanServiceImpl.findByPlanID(iPlanID);
			tTrainOrder.setCreateDate(new Date());
			tTrainOrder.setReserveDate(new Date());
			tTrainOrder.setFloor(tTrainPlan.getFloor());
			tTrainOrder.setPeer(tTrainPlan.getPeer());
			tTrainOrder.setPrice(tTrainPlan.getPrice());
			BigDecimal bPrice = tTrainPlan.getPrice();

			tTrainOrder.setOrderAmount(bPrice);
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			tTrainOrder.setCreator(tSysUser.getUserName());
			tTrainOrder.setReserveUserID(tSysUser.getUserID());
			tTrainOrder.setOrderID(cOrderID);
			trainOrderServiceImpl.save(tTrainOrder.getID());
			tTrainOrder.setOrderState((byte) 0);
			tTrainOrder.setCashierState((byte) 0);
			tTrainOrder.setAccountState((byte) 0);
			tTrainOrder.setInvoiceState((byte) 0);
			tTrainOrder.setRefundState((byte) 0);
			tTrainOrder.setRemittanceState((byte) 0);
			trainOrderServiceImpl.save(tTrainOrder);
			return "redirect:trainbaseinfo?iTrainID=" + Integer.parseInt(TrainID);

		} catch (Exception e) {

		}
		return null;

	}

	/**
	 * 函数功能说明： 预留转订单信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/enterpayordertrain")
	public String EnterPayOrderTrain(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));
		TTrainOrder tTrainOrder = trainOrderServiceImpl.findByID(iID);
		model.addAttribute("tTrainOrder", tTrainOrder);

		int iTrainID = Integer.parseInt(request.getParameter("iTrainID"));
		model.addAttribute("iTrainID", iTrainID);

		return "webbackground/train/payordertrain";

	}

	@RequestMapping(value = "/subpayordertrain")
	public String PayOrderTrain(TTrainOrder tTrainOrder, HttpSession session, String TrainID) throws Exception {

		try {
//			 定义一个新的实体类对象 
			TTrainOrder dtTrainOrder = trainOrderServiceImpl.findByID(tTrainOrder.getID());

			dtTrainOrder.setPayType(tTrainOrder.getPayType());
			dtTrainOrder.setOrderState((byte) 2);
			dtTrainOrder.setOrderDate(new Date());
			dtTrainOrder.setOrderName(tTrainOrder.getOrderName());
			dtTrainOrder.setOrderTel(tTrainOrder.getOrderTel());
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			dtTrainOrder.setOrderUserID(tSysUser.getUserID());
			dtTrainOrder.setReceivable(tTrainOrder.getReceivable());
			dtTrainOrder.setVoucher(tTrainOrder.getVoucher());
			dtTrainOrder.setReceipt(tTrainOrder.getReceipt());
			trainOrderServiceImpl.save(dtTrainOrder);

			return "redirect:printtrain?iID=" + tTrainOrder.getID();

		} catch (Exception e) {

		}
		return null;

	}

	/**
	 * 函数功能说明： 取消订单
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/entercancelordertrain")
	public String EnterCancelOrderTrain(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));
		TTrainOrder tTrainOrder = trainOrderServiceImpl.findByID(iID);
		model.addAttribute("tTrainOrder", tTrainOrder);

		int iTrainID = Integer.parseInt(request.getParameter("iTrainID"));
		model.addAttribute("iTrainID", iTrainID);

		return "webbackground/train/cancelordertrain";

	}

	@RequestMapping(value = "/subcancelordertrain")
	public String CancelOrderTrain(TTrainOrder tTrainOrder, HttpSession session, String TrainID) throws Exception {

		try {
//			 定义一个新的实体类对象 
			TTrainOrder dtTrainOrder = trainOrderServiceImpl.findByID(tTrainOrder.getID());

			dtTrainOrder.setOrderState((byte) 4);
			dtTrainOrder.setRefund(tTrainOrder.getRefund());
			trainOrderServiceImpl.save(dtTrainOrder);

			return "redirect:printcanceltrain?iID=" + tTrainOrder.getID();

		} catch (Exception e) {

		}
		return null;

	}

	/**
	 * 函数功能说明： 修改火车票预留信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/enterupdatereservetrain")
	public String EnterUpdateReserveTrain(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));

		TTrainOrder tTrainOrder = trainOrderServiceImpl.findByID(iID);
		model.addAttribute("tTrainOrder", tTrainOrder);
		int iTrainID = Integer.parseInt(request.getParameter("iTrainID"));
		model.addAttribute("iTrainID", iTrainID);

		return "webbackground/train/updatereservetrain";

	}

	@RequestMapping(value = "/updatereservetrain")
	public String UpdateReserveTrainBase(TTrainOrder tTrainOrder, HttpSession session, String TrainID)
			throws Exception {
		try {
//			 定义一个新的实体类对象 
			TTrainOrder dtTrainOrder = trainOrderServiceImpl.findByID(tTrainOrder.getID());
			dtTrainOrder.setReserveName(tTrainOrder.getReserveName());
			dtTrainOrder.setReserveTel(tTrainOrder.getReserveTel());
			dtTrainOrder.setMemo(tTrainOrder.getMemo());
			dtTrainOrder.setModifyDate(new Date());

			BigDecimal bPrice = dtTrainOrder.getPrice();

			dtTrainOrder.setOrderAmount(bPrice);
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			dtTrainOrder.setReviser(tSysUser.getUserName());
			trainOrderServiceImpl.save(dtTrainOrder);
			return "redirect:trainbaseinfo?iTrainID=" + Integer.parseInt(TrainID);

		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 函数功能说明： 取消预留
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/entercancelreservetrain")
	public String CancelReserveTrain(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));

		TTrainOrder tTrainOrder = trainOrderServiceImpl.findByID(iID);

		tTrainOrder.setOrderState((byte) 3);

		trainOrderServiceImpl.save(tTrainOrder);

		model.addAttribute("tTrainOrder", tTrainOrder);
		int iTrainID = Integer.parseInt(request.getParameter("iTrainID"));
		model.addAttribute("iTrainID", iTrainID);

		TTrainBase tTrainBase = trainBaseServiceImpl.findByID(iTrainID);
		model.addAttribute("tTrainBase", tTrainBase);

		List<VTrainOrder> lstTrainOrder = trainOrderServiceImpl.findByTrainID(iTrainID);
		model.addAttribute("orderlist", lstTrainOrder);

		return "webbackground/train/trainbaseinfo";

	}

	/**
	 * 函数功能说明： 修改火车票订单信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/enterupdateordertrain")
	public String EnterUpdateOrderTrain(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));

		TTrainOrder tTrainOrder = trainOrderServiceImpl.findByID(iID);
		model.addAttribute("tTrainOrder", tTrainOrder);
		int iTrainID = Integer.parseInt(request.getParameter("iTrainID"));
		model.addAttribute("iTrainID", iTrainID);

		return "webbackground/train/updateordertrain";

	}

	@RequestMapping(value = "/updateordertrain")
	public String UpdateOrderTrainBase(TTrainOrder tTrainOrder, HttpSession session, String TrainID) throws Exception {
		try {
//			 定义一个新的实体类对象 
			TTrainOrder dtTrainOrder = trainOrderServiceImpl.findByID(tTrainOrder.getID());
			dtTrainOrder.setOrderName(tTrainOrder.getOrderName());
			dtTrainOrder.setOrderTel(tTrainOrder.getOrderTel());
			dtTrainOrder.setMemo(tTrainOrder.getMemo());
			dtTrainOrder.setModifyDate(new Date());

			BigDecimal bPrice = dtTrainOrder.getPrice();
			dtTrainOrder.setOrderAmount(bPrice);
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			dtTrainOrder.setReviser(tSysUser.getUserName());
			trainOrderServiceImpl.save(dtTrainOrder);
			return "redirect:trainbaseinfo?iTrainID=" + Integer.parseInt(TrainID);

		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 函数功能说明： 删除取消预留信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/delreservetrainbase")
	public String DelReserveTrainBase(String checkList, HttpServletRequest request, Model model) {

		String[] checklist = checkList.split(",");
		List<Integer> lstID = new ArrayList<>();

		for (String str : checklist) {
			lstID.add(Integer.parseInt(str));
		}

		trainOrderServiceImpl.delTrainOrder(lstID);

		int iTrainID = Integer.parseInt(request.getParameter("iTrainID"));
		model.addAttribute("iTrainID", iTrainID);
		TTrainBase tTrainBase = trainBaseServiceImpl.findByID(iTrainID);
		model.addAttribute("tTrainBase", tTrainBase);

		List<VTrainOrder> lstTrainOrder = trainOrderServiceImpl.findByTrainID(iTrainID);
		model.addAttribute("orderlist", lstTrainOrder);

		return "webbackground/train/trainbaseinfo";
	}

	/**
	 * 函数功能说明： 删除取消订单信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/delordertrainbase")
	public String DelOrderTrainBase(String checkList, HttpServletRequest request, Model model) {

		String[] checklist = checkList.split(",");
		List<Integer> lstID = new ArrayList<>();

		for (String str : checklist) {
			lstID.add(Integer.parseInt(str));
		}

		trainOrderServiceImpl.delTrainOrder(lstID);

		int iTrainID = Integer.parseInt(request.getParameter("iTrainID"));
		model.addAttribute("iTrainID", iTrainID);

		TTrainBase tTrainBase = trainBaseServiceImpl.findByID(iTrainID);
		model.addAttribute("tTrainBase", tTrainBase);

		List<VTrainOrder> lstTrainOrder = trainOrderServiceImpl.findByTrainID(iTrainID);
		model.addAttribute("orderlist", lstTrainOrder);

		return "webbackground/train/trainbaseinfo";
	}

	/**
	 * 函数功能说明： 前台个人中心点击取消专列预留
	 *
	 * @param 参数说明 tSysUser:用户类 用户 session
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 hanxl
	 * @date 创建日期 2020.10.29
	 */
	@RequestMapping(value = "/entertraincancelreserve")
	public String enterTrainCancelReserve(HttpServletRequest request, HttpSession session) {

		int iID = Integer.parseInt(request.getParameter("iID"));
		TSysUser sessionSysUser = (TSysUser) session.getAttribute("tSysUser");
		trainOrderServiceImpl.updateState(iID, sessionSysUser);
		return "redirect:mytrainreserve";
	}

	/**
	 * 函数功能说明： 前台个人中心点击申请专列退单
	 *
	 * @param 参数说明 tSysUser:用户类 用户 session
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 hanxl
	 * @date 创建日期 2020.10.29
	 */
	@RequestMapping(value = "/entertrainapplyrefund")
	public String enterTrainApplyRefund(HttpServletRequest request, HttpSession session) {

		int iID = Integer.parseInt(request.getParameter("iID"));
		TSysUser sessionSysUser = (TSysUser) session.getAttribute("tSysUser");
		trainOrderServiceImpl.updateRefundState(iID, sessionSysUser);
		return "redirect:mytrainorder";
	}

	/**
	 * 函数功能说明： 前台个人中心点击撤销专列退单
	 *
	 * @param 参数说明 tSysUser:用户类 用户 session
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 hanxl
	 * @date 创建日期 2020.10.29
	 */
	@RequestMapping(value = "/entertraincancelrefund")
	public String enterTrainCancelRefund(HttpServletRequest request, HttpSession session) {

		int iID = Integer.parseInt(request.getParameter("iID"));
		TSysUser sessionSysUser = (TSysUser) session.getAttribute("tSysUser");
		trainOrderServiceImpl.updateCancelState(iID, sessionSysUser);
		return "redirect:mytrainrefund";
	}

	/* 前台个人中心显示订单详情 */
	@RequestMapping(value = "/mytraininfo")
	public String toMyTrainInfo(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));

		TTrainOrder dbTrainOrder = trainOrderServiceImpl.findByID(iID);
		model.addAttribute("dbTrainOrder", dbTrainOrder);

		String strOrderID = dbTrainOrder.getOrderID();
		int iPlanID = Integer.parseInt(strOrderID.substring(6, 12));

		VTrainPlan dbTrainPlan = trainPlanServiceImpl.findByID(iPlanID);

		model.addAttribute("dbTrainPlan", dbTrainPlan);

		return "permanagement/MyTrainInfo";
	}
}
